import Vue from 'vue'
import Vuex from 'vuex'

import { getMusic, postNotes, getNotes } from '../utils/api'
Vue.use(Vuex)

var calcType = (self, user) => {
  if (user === 'system') return 'system'
  if (user === self) return 'self'
  return 'other'
}

export default new Vuex.Store({
  state: {
    user: '用户#' + parseInt(Math.random() * 1000),
    messages: [],
    netState: 'init',
    wwapStatus: 'collapse',
    blackboard: {
      open: false,
    },
    music: [],
    notes: [],
  },
  mutations: {
    ADD_MESSAGE (state, { user, message, dtype, data }) {
      if (!message) return false
      if ((dtype || 'text') !== 'text' && !data) return false// 如果不是文字信息且未提供数据则返回
      state.messages.push({
        id: state.messages.length,
        data: {
          user,
          message,
          show: true,
          type: calcType(state.user, user),
          dtype: dtype || 'text',
          data: data || null,
        },
      })
      return true
    },
    DEL_MESSAGE (state, id) {
      state.messages.splice(id, 1)
    },
    SET_NET_STATE (state, netState) {
      // init
      // connected
      // server_disconnect
      // network_error
      state.netState = netState
    },
    SET_WWAP_STATUS (state, status) {
      state.wwapStatus = status
    },
    SET_MUSIC (state, music) {
      state.music = music
    },
    TOGGLE_BLACKBOARD (state, open) {
      state.blackboard.open = open
    },
    UPDATE_NOTE (state, { idx, note }) {
      state.notes[idx] = note
      state.notes.push(1)
      state.notes.pop(1)
    },
    ADD_NOTE (state) {
      state.notes.push(
        {
          type: 'text',
          position: {
            x: 100 + 200 * Math.random(),
            y: 100 + 200 * Math.random(),
          },
          width: 300,
          color: '#fff',
          content: '新笔记',
        })
    },
    SET_NOTES (state, notes) {
      state.notes = notes
    },
    DELETE_NOTE (state, idx) {
      state.notes.splice(idx, 1)
    },
  },
  actions: {
    async getMusic ({ commit, dispatch }, { err, counter, interval }) {
      if (counter < 0) { return }
      getMusic().then(res => {
        commit('SET_MUSIC', res.data)
      }).catch(error => {
        window.setTimeout(() => {
          dispatch('getMusic', {
            err: error,
            counter: counter - 1,
            interval,
          })
        }, interval)
      })
    },
    async postNotes ({ state }) {
      if (!state.notes.length) return
      var res = await postNotes(state.notes)
      res = res.data
      if (!res.success) {
        throw res
      }
    },
    async getNotes ({ commit, dispatch }, { err, counter, interval }) {
      if (counter < 0) { return }
      getNotes().then(res => {
        commit('SET_NOTES', res.data)
      }).catch(error => {
        window.setTimeout(() => {
          dispatch('getNotes', {
            err: error,
            counter: counter - 1,
            interval,
          })
        }, interval)
      })
    },
  },
  modules: {
  },
})
